package com.adb.receivecontent;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class SharePictureActivity extends Activity {

	private final String TAG = SharePictureActivity.class.getCanonicalName();
	private static final int MENU_ITEM_1 = Menu.FIRST;
	private static final int MENU_ITEM_2 = MENU_ITEM_1 + 1;
	private static final int MENU_ITEM_3 = MENU_ITEM_2 + 1;
	private static final int MENU_ITEM_4 = MENU_ITEM_3 + 1;
	
	TextView textView_form;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "[ onCreate ]");
		setContentView(R.layout.activity_share_picture);
		
		textView_form = (TextView)findViewById(R.id.textView_form);
		
		Intent intent = getIntent();
		String mAction = intent.getAction();
		String mType = intent.getType();

		if (Intent.ACTION_SEND.equals(mAction) && mType != null) {
			if ("text/plain".equals(mType)) {
				handleSendText(intent); // Handle text being sent
			} else if (mType.startsWith("image/")) {
				handleSendImage(intent); // Handle single image being sent
			}

		} else if (Intent.ACTION_SEND_MULTIPLE.equals(mAction) && mType != null) {
			if (mType.startsWith("image/")) {
				handleSendMultipleImages(intent); // Handle multiple images
			}
		} else {

		}
		
		
		registerForContextMenu(textView_form);
		
		
	}

	void handleSendText(Intent intent) {
		String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
		if (sharedText != null) {
			// Update UI to reflect text being shared
		}
	}

	void handleSendImage(Intent intent) {
		Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
		if (imageUri != null) {
			// Update UI to reflect image being shared
		}
	}

	void handleSendMultipleImages(Intent intent) {
		ArrayList<Uri> imageUris = intent
				.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
		if (imageUris != null) {
			// Update UI to reflect multiple images being shared
		}
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Complete action using below :");
		menu.add(0, MENU_ITEM_1, 0, "Form A");
		menu.add(0, MENU_ITEM_2, 1, "Form B");
		menu.add(0, MENU_ITEM_3, 2, "Form c");
		menu.add(0, MENU_ITEM_4, 3, "Form D");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		return super.onContextItemSelected(item);
		
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "[ onStart ]");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				if(textView_form != null)
				textView_form.performLongClick();
			}
		}, 1000);
		
		
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "[ onStop ]");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "[ onDestroy ]");
	}
}
